package com.dlc.felear.xiaoensale.customView;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.MenuTypeItemAdapter;
import com.dlc.felear.xiaoensale.entity.ActionStepItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class JbSelectDialog extends Dialog implements AdapterView.OnItemClickListener ,View.OnClickListener{
    private static final String TAG = "SelectGridviewDialog";
    private GridView gv;
    private Context context;
    private JbSelectDialogCallback callback;
    private ActionStepItem actionStepItem;
    private MenuTypeItemAdapter adapter;
    private int level=0;
    private int direct=2;
    private HashMap<Integer,View> hashMapView=new HashMap<>();

    public JbSelectDialog(@NonNull Context context, JbSelectDialogCallback callback) {
        super(context);
        this.callback = callback;
        initView(context);
    }


    public JbSelectDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        View root = LayoutInflater.from(context).inflate(R.layout.jiaoban_select_gridview_dialog, null);
        root.findViewById(R.id.tv_zf).setOnClickListener(this);
        root.findViewById(R.id.tv_fz).setOnClickListener(this);
        gv = root.findViewById(R.id.gv);
        adapter = new MenuTypeItemAdapter(context);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(this);
        setContentView(root);
        setCanceledOnTouchOutside(true);
    }


    public void showDialog(ActionStepItem actionStepItem) {
        level=0;
        this.actionStepItem=actionStepItem;
        adapter.setItems(getPowerLevel());
        show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }


    private void confirmData() {
        dismiss();
    }


    private void showToast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            String value = adapter.getData().get(position);
            level=Integer.parseInt(value.substring(0,1));
            adapter.setOnItemBg(value);
        } catch (Exception e) {
            showToast(e.getMessage());
        }
    }


    @Override
    public void onClick(View v) {
        if(v.getId()== R.id.tv_zf){
            direct =2;
        }else if(v.getId()== R.id.tv_fz){
            direct=1;
        }
        if(level<=0){
            showToast("请选择档位");
          return;
        }
        if (callback != null) {
            callback.onJbItemClickResult(actionStepItem,level,direct);
        }
        confirmData();
    }

    public interface JbSelectDialogCallback {
        void onJbItemClickResult(ActionStepItem actionStepItem, int result, int direct);
    }

    private List<String>  getPowerLevel(){
        List<String> temp=new ArrayList<>();
        temp.add("1档");
        temp.add("2档");
        temp.add("3档");
        temp.add("4档");
        temp.add("5档");
        temp.add("6档");
        temp.add("7档");
        temp.add("8档");
        temp.add("9档");
        return temp;
    }
}
